package com.clx.channelHandler.hander;


import com.clx.CrpcBootstrap;
import com.clx.transport.message.CrpcResponse;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.util.concurrent.CompletableFuture;

/**
 * 这是一个用来测试的类
 * */
@Slf4j
public class MySimpleChannelInboundHandler extends SimpleChannelInboundHandler<CrpcResponse> {
            @Override
            protected void channelRead0(ChannelHandlerContext channelHandlerContext, CrpcResponse crpcResponse) throws Exception {

                // 服务提供方，给予的结果
                Object returnValue = crpcResponse.getBody();

                // todo 需要针对code做处理
                returnValue = returnValue == null ? new Object() : returnValue;
                // 从全局的挂起的请求中寻找与之匹配的待处理的 completableFuture
                CompletableFuture<Object> completableFuture = CrpcBootstrap.PENDING_REQUEST.get(crpcResponse.getRequestId());
                completableFuture.complete(returnValue);
                if (log.isDebugEnabled()) {
                    log.debug("以寻找到编号为【{}】的completableFuture。处理响应结果。", crpcResponse.getRequestId());
                }
    }
}
